package com.gitee.ricesugar.book.module;

import com.gitee.ricesugar.entity.Book;
import com.gitee.ricesugar.mapper.BookMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class ListBook extends JPanel {
    private List<Book> bookList;
    private JTextField searchField;
    private DefaultTableModel tableModel;
    private JTable bookTable;
    private BookMapper bookMapper;

    public ListBook() throws IOException {

        createSqlSession();

        bookList = bookMapper.listByTitle("");

        setLayout(new BorderLayout());

        // 搜索框面板
        JPanel searchPanel = new JPanel();
        searchPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        JLabel searchLabel = new JLabel("搜索用户名：");
        searchPanel.add(searchLabel);

        searchField = new JTextField(20);
        searchPanel.add(searchField);

        JButton searchButton = new JButton("搜索");
        searchPanel.add(searchButton);

        add(searchPanel, BorderLayout.NORTH);

        // 用户列表
        tableModel = new DefaultTableModel();
        tableModel.addColumn("ID");
        tableModel.addColumn("Title");
        tableModel.addColumn("Author");
        tableModel.addColumn("Press");
        tableModel.addColumn("ISBN");
        tableModel.addColumn("Category");
        tableModel.addColumn("Description");
        tableModel.addColumn("Amount");

        int idCounter = 1;
        for (Book obj : bookList) {
            Object[] rowData = {idCounter++, obj.getTitle(), obj.getAuthor(), obj.getPress(), obj.getIsbn(), obj.getCategory(), obj.getDescription(), obj.getAmount()};
            tableModel.addRow(rowData);
        }

        bookTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(bookTable);
        add(scrollPane, BorderLayout.CENTER);

        // 搜索按钮点击事件
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = searchField.getText().trim();

                bookList = bookMapper.listByTitle(username);

                tableModel.setRowCount(0);
                int idCounter = 1;
                for (Book obj : bookList) {
                    Object[] rowData = {idCounter++, obj.getTitle(), obj.getAuthor(), obj.getPress(), obj.getIsbn(), obj.getCategory(), obj.getDescription(), obj.getAmount()};
                    tableModel.addRow(rowData);
                }
            }
        });
    }


    private void createSqlSession() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        bookMapper = sqlSession.getMapper(BookMapper.class);
    }
}
